package com.lc.ibps.platform.rest.system;

import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.api.common.cat.constants.CategoryConstants;
import com.lc.ibps.api.common.cat.model.IDictionary;
import com.lc.ibps.api.common.cat.model.IType;
import com.lc.ibps.api.common.cat.service.IDictionaryService;
import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.common.cat.service.impl.DefaultTypeService;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 数据字典接口
 *
 * <pre>
 *  
 * 构建组：ibps-platform-webapi
 * 作者：zhongjh
 * 邮箱：zjh20140614@163.com
 * 日期：2017年8月31日-上午11:48:09
 * 版权：广州流辰信息技术有限公司版权所有
 * </pre>
 */
@RequestMapping(value = "/api/webapi/dictionaryService")
@Api(value = "数据字典服务", tags = "系统管理-数据字典服务")
@RestController
public class DictionaryController extends BaseApiController {

	private IDictionaryService dictionaryService;
	private DefaultTypeService typeService;

	public DictionaryController() {
		dictionaryService = AppUtil.getBean(IDictionaryService.class);
		typeService = AppUtil.getBean(DefaultTypeService.class);
	}

	@ApiOperation(value = "获取数据字典值", notes = "数据字典服务")
	@RequestMapping(value = "/getValByTypeKey", method = RequestMethod.GET)
	public APIResult<String> getValByTypeKey(
			@ApiParam(name = "type", value = "分类表key值", required = true) @RequestParam(name = "type", required = true) String type,
			@ApiParam(name = "key", value = "数据字典表key值", required = true) @RequestParam(name = "key", required = true) String key) {
		APIResult<String> result = new APIResult<>();
		try {
			String name = dictionaryService.getLabelByKey(type, key);
			result.setData(name);
			result.setMessage("获取数据字典值成功！");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取数据字典值失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "获取数据字典", notes = "数据字典服务")
	@RequestMapping(value = "/getDicByType", method = RequestMethod.GET)
	public APIResult<List<IDictionary>> getDicByType(
			@ApiParam(name = "type", value = "分类表key值", required = true) @RequestParam(name = "type", required = true) String type) {
		APIResult<List<IDictionary>> result = new APIResult<>();
		try {
			List<IDictionary> data = dictionaryService.findByType(type);
			result.setData(data);
			result.setMessage("获取数据字典成功！");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取数据字典成功！", e);
		}
		return result;
	}

	@ApiOperation(value = "获取数据字典类型", notes = "数据字典服务")
	@RequestMapping(value = "/getDicInfo", method = RequestMethod.GET)
	public APIResult<IType> getDicInfo(
			@ApiParam(name = "typeKey", value = "分类表key值", required = true) @RequestParam(name = "typeKey", required = true) String typeKey) {
		APIResult<IType> result = new APIResult<>();
		try {
			IType data = typeService.getByCategoryKeyAndTypeKey(CategoryConstants.CAT_DIC.key(), typeKey);
			result.setData(data);
			result.setMessage("获取数据字典类型成功！");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取数据字典类型失败！", e);
		}
		return result;
	}

}
